package com.itlong.cloud.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itlong.cloud.POJO.DTO.property.HouseHoldManageJPushDTO;
import com.itlong.cloud.POJO.PO.TimeTaskDeadMessageQueuePO;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.push.JPushClientService;
import com.rabbitmq.client.Channel;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      物业住户迁入/迁出/审核时极光推送业务操作队列消费者
 * </desc>
 *
 * @createDate 16:49 2017-11-30
 */
@Component
public class HouseHoldJPushConsumer {
    private static final Logger LOG = LoggerFactory.getLogger(HouseHoldJPushConsumer.class);

    @Autowired
    JPushClientService jPushClientService;


    /**
     * <desc>
     *      物业住户迁入/迁出/审核时极光推送业务操作队列消费者
     * </desc>
     * @param message 消息
     * @param channel
     * @return
     * @author zhangs
     * @createDate 2017/11/30
     */
    @RabbitListener(queues = RabbitMQConstant.RABBITMQ_HOUSEHOLD_JPUSH_QUEUE)
    public void process(Message message, Channel channel) throws InterruptedException, IOException {
        byte[] body = message.getBody();
        LOG.info("RabbitMQ_物业住户迁入/迁出/审核时极光推送业务消费端:"+new String(body));
        try {
            TimeTaskDeadMessageQueuePO timeTaskDeadMessageQueuePO = JSON.parseObject(new String(body), TimeTaskDeadMessageQueuePO.class);
            JSONObject dataJs = JSON.parseObject(timeTaskDeadMessageQueuePO.getMessageContent());
            String string = dataJs.getString("data");
            List<HouseHoldManageJPushDTO> houseHoldManageJPushDTOS = JSON.parseArray(string, HouseHoldManageJPushDTO.class);
            if (houseHoldManageJPushDTOS != null && houseHoldManageJPushDTOS.size() > 0) {
                for (HouseHoldManageJPushDTO houseHoldManageJPushDTO:houseHoldManageJPushDTOS) {
                    //推送指令
                    Map<String,String> comands = houseHoldManageJPushDTO.getComands();
                    //推送消息
                    String msg = houseHoldManageJPushDTO.getMsg();
                    //用户手机号
                    String phoneNum = houseHoldManageJPushDTO.getPhoneNum();

                    //用户邮箱
                    String email = houseHoldManageJPushDTO.getEmail();

                    if (StringUtils.isNotBlank(email)){
                        jPushClientService.pushWithTaskTO(email,comands,msg);
                    }

                    if (StringUtils.isNotBlank(phoneNum)) {
                        jPushClientService.pushWithTaskTO(phoneNum, comands, msg);
                    }
                }
            }
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            LOG.error("【RabbitMQ_物业住户极光推送业务消费端】:解析异常",e);
        } finally {
            //手动确认消息消费成功。
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }
}
